//
//  KwAlertView.m
//  KuWoCan
//
//  Created by hwd on 17/3/16.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "KwAlertView.h"
#import "NwOrderRequest.h"

@interface KwAlertView ()

@property (nonatomic,strong) UILabel *titleLab;

@property (nonatomic,strong) UIView *otherView;

@end

@implementation KwAlertView

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    static KwAlertView *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[KwAlertView alloc] init];
    });
    return instance;
    
}

- (instancetype)init
{
    if (self = [super init])
    {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@30);
        make.right.equalTo(@-30);
        make.top.equalTo(@100);
        make.bottom.equalTo(@-100);
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = KwBlueColor;
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(30);
        make.right.equalTo(bgView.mas_right).offset(-30);
        make.height.equalTo(@2);
        make.top.equalTo(bgView.mas_top).offset(50);
    }];
    
    
    CGFloat labWidth = [KwSize boundingRectWithText:@"title" Font:[UIFont systemFontOfSize:15]].width;
    _titleLab = [UILabel new];
    _titleLab.backgroundColor = [UIColor whiteColor];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.font = KwFont(15);
    _titleLab.text = @"title";
    _titleLab.textColor = KwBlueColor;
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lineView.mas_centerX);
        make.width.equalTo(@(labWidth+15*2));
        make.top.equalTo(bgView.mas_top).offset(40);
        make.height.equalTo(@20);
    }];
    
    _otherView = [UIView new];
    [_otherView setBackgroundColor:[UIColor whiteColor]];
    [bgView addSubview:_otherView];
    [_otherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLab.mas_bottom).offset(30);
        make.bottom.equalTo(bgView.mas_bottom).offset(-60);
        make.left.right.equalTo(@0);
    }];
    
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100+i;
        btn.borderColor = [UIColor grayColor];
        btn.borderWidth = 1;
        btn.titleLabel.font = KwFont(15);
        [btn addTarget:self action:@selector(btnActionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btn];
        if (i == 0) {
            [btn setTitle:@"取消" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        else
        {
            [btn setTitle:@"确认" forState:UIControlStateNormal];
            [btn setTitleColor:KwBlueColor forState:UIControlStateNormal];
        }
    }
    
    for (int i = 0; i < 2; i++)
    {
        UIButton *btn = [self viewWithTag:100+i];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0)
            {
                make.left.equalTo(bgView.mas_left);
                make.right.equalTo(bgView.mas_centerX);
            }
            else
            {
                make.right.equalTo(bgView.mas_right);
                make.left.equalTo(bgView.mas_centerX);
            }
            make.height.equalTo(@40);
            make.bottom.equalTo(bgView.mas_bottom);
        }];
    }
}

- (void)btnActionWithBtn:(UIButton *)btn
{
    if (btn.tag == 100)
    {
        if (_block)
        {
            _block(0);
        }
        //取消
        [self removeFromSuperview];
    }
    else
    {
        if (_block)
        {
            _block(1);
        }
        //确认
        [self removeFromSuperview];
    }
    
}


- (void)showAlertViewWithTitle:(NSString *)title mainView:(UIView *)mainView
{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _titleLab.text = title;
    [_otherView addSubview:mainView];
    
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(@0);
    }];
    
    CGFloat labWidth = [KwSize boundingRectWithText:title Font:[UIFont systemFontOfSize:15]].width;
    
    [_titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(labWidth+15*2));
    }];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

@end
